clc
clear

 k=1;
 RM_ratio=zeros(1,8); 
 PT_ratio=zeros(1,8); 
 Total_utilization=0.8;
 
for task_Num=3:10
    task_Set_Num=1000;
    task_Set=zeros(task_Num,5);
    
    RM_schedulability=zeros(1,task_Set_Num);
    RM_schedulable_Num=0;
    
    PT_schedulability=zeros(1,task_Set_Num);
    PT_schedulable_Num=0;


    for n=1:task_Set_Num
        %Prepare Ci, Ci_0, Ti, Di, Li based on the implementation paper
        task_Utilization=rand(1,task_Num)';  
        task_Period=rand(1,task_Num)';
        task_Due_Time=task_Period;
        
        task_Exe_Time=task_Period*Total_utilization.*task_Utilization/sum(task_Utilization);
        task_Exe_Time_Overload=1.2*task_Exe_Time;


        task_Set(:,1)=task_Exe_Time;
        task_Set(:,2)=task_Exe_Time_Overload;
        task_Set(:,3)=task_Period;
        task_Set(:,4)=task_Due_Time;


        %disorder the criticality
        %{
        temp_Criticality=1:task_Num;
        N=length(temp_Criticality);
        b=randperm(N);
        task_Criticality=temp_Criticality(b)';
        %}
        %didn't lose generality, from low criticality to high criticality
        task_Criticality=1:task_Num;
        task_Set(:,5)=task_Criticality;

        RM_schedulability(n)=RM_schedulability_test(task_Set); 
        
        PT_schedulability(n)=PT_schedulability_test(task_Set); 
        
        %schedulability(n)=PT_schedulability_test(task_Set);

        if RM_schedulability(n)==1
            RM_schedulable_Num=RM_schedulable_Num+1;
        end
        
        if PT_schedulability(n)==1
            PT_schedulable_Num=PT_schedulable_Num+1;
        end

    end

    %schedulability
    RM_ratio(k)=RM_schedulable_Num/task_Set_Num;
    PT_ratio(k)=PT_schedulable_Num/task_Set_Num;
    k=k+1;
end

figure(1)
plot(3:10,RM_ratio*task_Set_Num,'--rs','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10)
hold on
plot(3:10,PT_ratio*task_Set_Num,'-b^','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10)
            
xlabel('Number of tasks')
ylabel('Schedulable cases')
ylim([0 task_Set_Num])
legend('RM','PT')
title(['Schedulability Evaluation with Utilization ', num2str(Total_utilization)])

